package com.zhouxiaoge.lambda;

import org.junit.Test;

import java.io.PrintStream;
import java.util.Comparator;
import java.util.function.*;

/****************************************************************
 卐卍卐卍卐卍卐卍卐卍卐卍卐卍卐卍卐卍卐卍卐卍卐卍卐卍卐卍卐卍卐卍卐卍
 卐卐卐                    _ooOoo_                    卍卍卍卍卍卍
 卐卐卐      卐           o8888888o          卍       * 佛  卍卍卍
 卐卐卐                  88'' . ''88                  * 祖  卍卍卍
 卐卐卐                  ( | -_- | )                  * 保  卍卍卍
 卐卐卐                   0\  =  /0                   * 佑  卍卍卍
 卐卐卐                ____/'---'\____                *     卍卍卍
 卐卐卐              .'  \\|     |//  '.              * 永  卍卍卍
 卐卐卐             /  \\|||  :  |||//  \             * 无  卍卍卍
 卐卐卐            /  _||||| -:- |||||-  \            * BUG 卍卍卍
 卐卐卐            |   | \\\  -  /// |   |            *     卍卍卍
 卐卐卐            | \_|  ''\---/''  |   |            * 阿  卍卍卍
 卐卐卐            \  .-\__  `-`  ___/-. /            * 弥  卍卍卍
 卐卐卐          ___`. .'  /--.--\  `. . __           * 陀  卍卍卍
 卐卐卐        ."" '<  ‘.___\_<|>_/___.’  >'"".     * 佛  卍卍卍
 卐卐卐       | | :  ` - `.;`\ _ /`;.`/ - ` : | |     卍卍卍卍卍卍
 卐卐卐       | \ `-.   \_ __\ /__ _/   .-`  / /      卍卍卍卍卍卍
 卐卐卐 ======`-.____`-.___\_____/___.-`____.-'=======卍卍卍卍卍卍
 卐@author 周明阳 mail:gqzmy@outlook.com 时间:2019-06-21 16:09卍卍
 卐卍卐卍卐卍卐卍卐卍卐卍卐卍卐卍卐卍卐卍卐卍卐卍卐卍卐卍卐卍卐卍卐卍
 *
 *
 */

public class MethodReferencesTest {
    // 对象::实例方法名
    @Test
    public void test1() {
        Consumer<String> consumer1 = x -> System.out.println(x);
        consumer1.accept("Hello World !!! 111 ");
        PrintStream printStream = System.out;
        Consumer<String> consumer2 = x -> printStream.println(x);
        consumer2.accept("Hello World !!! 222 ");
        Consumer<String> consumer3 = printStream::println;
        consumer3.accept("Hello World !!! 333 ");

        Employee employee = new Employee(102, "马八", 12, 111.111);
        employee.setName("11111");
        Supplier<String> supplier1 = () -> employee.getName();
        System.out.println(supplier1.get());
        Supplier<String> supplier2 = employee::getName;
        System.out.println(supplier2.get());
    }

    // 类::静态方法名
    @Test
    public void test2() {
        Comparator<Integer> comparator1 = (x, y) -> Integer.compare(x, y);
        System.out.println(comparator1.compare(1, 2));

        Comparator<Integer> comparator2 = Integer::compare;
        System.out.println(comparator2.compare(1, 2));

    }

    // 类::实例方法名
    @Test
    public void test3() {
        BiPredicate<String, String> biPredicate1 = (x, y) -> x.equals(y);
        System.out.println(biPredicate1.test("a", "a"));

        BiPredicate<String, String> biPredicate2 = String::equals;
        System.out.println(biPredicate2.test("a", "a"));
    }

    // 构造器引用
    @Test
    public void test4() {
        Supplier<Employee> supplier1 = () -> new Employee();
        Employee employee1 = supplier1.get();
        System.out.println(employee1);

        Supplier<Employee> supplier2 = Employee::new;
        Employee employee2 = supplier2.get();
        System.out.println(employee2);

        Supplier<Employee> supplier3 = () -> new Employee(1, "猴十", 1, 11);
        Employee employee3 = supplier3.get();
        System.out.println(employee3);

        // 一个参数的构造器
        Function<Integer, Employee> function1 = x -> new Employee(x);
        Employee apply1 = function1.apply(101);

        System.out.println(apply1);

        // 一个参数的构造器
        Function<Integer, Employee> function2 = Employee::new;
        Employee apply2 = function2.apply(102);
        System.out.println(apply2);

        // 两个参数的构造器
        BiFunction<Integer, Integer, Employee> biFunction = Employee::new;
        Employee apply3 = biFunction.apply(101, 29);
        System.out.println(apply3);
    }

    // 数组引用
    @Test
    public void test5() {
        Function<Integer, String[]> function1 = x -> new String[x];
        String[] strings1 = function1.apply(10);
        System.out.println(strings1.length);

        Function<Integer, String[]> function2 = String[]::new;
        String[] strings2 = function2.apply(10);
        System.out.println(strings2.length);

    }
}
